package com.polimi.meteocal.security;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import com.polimi.meteocal.business.control.EventManager;
import com.polimi.meteocal.business.control.UserManager;
import com.polimi.meteocal.business.entity.Event;
import com.polimi.meteocal.business.entity.Notification;

@ManagedBean
@RequestScoped
public class WeatherBean {
		
	
	@EJB
	EventManager em;
	@EJB
	UserManager um;
		//String that contains the weather conditional message to be displayed in the dialog
		private String msg;
		
		//List of invites that the user hasn't already seen
		private List<Notification> unreadNotifications;
		
		private List<Event> relatedEvents;
		
		@PostConstruct
		public void init(){
			this.unreadNotifications= em.getUnreadNotifications(um.getLoggedUser().getEmail());
			int count= this.unreadNotifications.size();
			if(count>1)
				this.msg= "You have "+count+" new weather notifications";
			else if(count==1)
				this.msg= "You have "+count+" new weather notification";
			else
				this.msg= "You don't have UNREAD weather notifications";
			
			
			FacesContext context = FacesContext.getCurrentInstance();
	        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
	        ServletContext sContext = request.getSession().getServletContext();
	        sContext.setAttribute("unreadNotifications", this.unreadNotifications);
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		public List<Event> getRelatedEvents() {
			return relatedEvents;
		}

		public void setRelatedEvents(List<Event> relatedEvents) {
			this.relatedEvents = relatedEvents;
		}

		public List<Notification> getUnreadNotifications() {
			return unreadNotifications;
		}

		public void setUnreadNotifications(List<Notification> unreadNotifications) {
			this.unreadNotifications = unreadNotifications;
		}
}
